#include <bits/stdc++.h>
using namespace std;
const int N=1050;
int a[N],b[N];
int main(void){
    //freopen("data.txt","r",stdin);
    int i=0;
    while(~scanf("%d%d",&a[i],&b[i])){
        i++;
    }
    vector<int> res;
    int idx=b[0];
    while(idx!=0){
        res.push_back(a[idx]);
        idx=b[idx];
    }
    int siz=res.size();
    printf("%d\n",siz);
    for(int i=0;i<siz;i++){
        printf("%d",res[i]);
        if(i!=siz-1){
            printf(" ");
        }
    }
    printf("\n");
    return 0;
}